package com.hktf.uilib.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.UIUtils;
import com.hktf.corelib.R;
import com.hktf.uilib.utils.DisplayUtil;

import org.apache.commons.lang3.StringUtils;

/**
 * @包名称： com.yx.uilib.utils
 * @类名： ImageUtil
 * @创建人： 陈大专
 * @时间： 2020/11/30 14:57
 * @描述：
 */
public class ImageUtilNew {

    /**
     * 加载相册目录
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    public static void loadFolderImage(@NonNull final Context context, @NonNull String url,int width,int height, @NonNull final ImageView imageView) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .override(width, height)
                .centerCrop()
                .sizeMultiplier(0.5f)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(UIUtils.dip2px(8))))
                .placeholder(com.hktf.corelib.R.drawable.ic_launcher)
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.
                                        create(context.getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(30);
                        imageView.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }
    /**
     * 加载相册目录
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    public static void loadImageFitXY(@NonNull final Context context, @NonNull String url,int width,int height, @NonNull final ImageView imageView) {
        Glide.with(context)
                .load(url)
                .override(width, height)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(UIUtils.dip2px(8))))
                .placeholder(com.hktf.corelib.R.drawable.ic_launcher)
                .into(imageView);
    }
    /**
     * 加载相册目录
     *
     * @param context   上下文
     * @param imageView 承载图片ImageView
     */
    public static void loadImageFitXY(@NonNull final Context context, int redId,int width,int height, @NonNull final ImageView imageView) {
        Glide.with(context)
                .load(redId)
                .override(width, height)
                .placeholder(R.drawable.ic_launcher)
                .into(imageView);
    }
    /**
     * 加载相册目录
     *
     * @param context   上下文
     * @param imageView 承载图片ImageView
     */
    public static void loadImage(@NonNull final Context context, int redId,@NonNull final ImageView imageView) {
        Glide.with(context)
                .load(redId)
                .placeholder(R.drawable.ic_launcher)
                .into(imageView);
    }


    public static void loadCircleImage(ImageView iv,@DrawableRes int defaultResourceId,String path){
        if (StringUtils.isEmpty(path)) {
            iv.setImageResource(defaultResourceId);
            return;
        }
        RequestOptions options = new RequestOptions()
                .placeholder(defaultResourceId)
                .fitCenter()
                .circleCropTransform();
        Glide.with(CommUtils.getContext())
                .load(path)
                .apply(options)
                .into(iv);
    }

    public static void loadWithCorner(ImageView iv,String path,int cornerDp){
        Glide.with(CommUtils.getContext())
                .asBitmap()
                .load(path)
                .centerCrop()
                .placeholder(R.drawable.ic_launcher)
                .into(new BitmapImageViewTarget(iv) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.
                                        create(CommUtils.getContext().getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(UIUtils.dip2px(cornerDp));
                        iv.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }
    public static void loadWithCorner(ImageView iv,int redsId,int cornerDp){
        Glide.with(CommUtils.getContext())
                .asBitmap()
                .load(redsId)
                .centerCrop()
                .placeholder(R.drawable.ic_launcher)
                .into(new BitmapImageViewTarget(iv) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.
                                        create(CommUtils.getContext().getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(UIUtils.dip2px(cornerDp));
                        iv.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }
}
